package com.test;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class T3ThreadPrint2 {
    public static void main(String[] args) {

        SynchronousQueue<Long> sq1 = new SynchronousQueue<Long>();
        SynchronousQueue<Long> sq2 = new SynchronousQueue<Long>();
        SynchronousQueue<Long> sq3 = new SynchronousQueue<Long>();

        Thread t1 = new Thread(new Runnable() {
            String prefix = "A";
            @Override
            public void run() {
                int no=1;
                while(true){
                    System.out.println(prefix+no+" "+System.currentTimeMillis()/100);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    try {
                        sq1.put(1L);
                        sq3.take();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    no++;

                }
            }
        });

        Thread t2 = new Thread(new Runnable() {
            String prefix = "B";
            @Override
            public void run() {
                int no=1;
                while(true){
                    try {
                        sq1.take();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(prefix+no+" "+System.currentTimeMillis()/100);
                    try {
                        Thread.sleep(500);
                        sq2.put(1L);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    no++;

                }
            }
        });


        Thread t3 = new Thread(new Runnable() {
            String prefix = "C";
            @Override
            public void run() {
                int no=1;
                while(true){
                    try {
                        sq2.take();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(prefix+no+" "+System.currentTimeMillis()/100);
                    try {
                        Thread.sleep(500);
                        sq3.put(1L);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    no++;

                }
            }
        });

        t1.start();
        t2.start();
        t3.start();

        try {
            t1.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }
}
